package com.example.demo.interceptor;

import com.example.demo.common.Constant;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.SessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.example.demo.common.Constant.SESSION_ID;

/**
 * @DESCRIPTION:
 * @AUTOOR: Lee
 * @DATE: 2022年09月18日 5:52
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Autowired
    private SessionMapper sessionMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // get token
        UserEntity user = sessionMapper.queryByToken(request.getHeader(SESSION_ID));
        if (user == null) {
            return false;
        }
        request.setAttribute(Constant.SESSION_USER, user);
        return true;
    }

}
